package com.ant.storage.controller;


import com.alibaba.excel.EasyExcel;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.ContractDto;
import com.ant.storage.excel.ContractManagementExecl;
import com.ant.storage.pojo.ContractManagement;
import com.ant.storage.service.IContractManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author melon
 * @since 2024-01-19
 */
@RestController
@RequestMapping("/contract")
public class ContractManagementController {

    @Autowired
    private IContractManagementService managementService;

    @ApiOperation(value = "获取合同数据列表")
    @GetMapping("/getContractList")
    public ResultPage getContractList(ContractDto dto) {
        return ResultPage.success("操作成功", managementService.queryList(dto));
    }

    @ApiOperation(value = "id查询详情")
    @GetMapping("/findContractById/{id}")
    public ResultVo findContractById(@PathVariable("id") Integer id) {
        return ResultVo.success("操作成功", managementService.findContractById(id));
    }

    @ApiOperation(value = "保存信息")
    @PostMapping("/contractSave")
    public ResultVo contractSave(@RequestBody ContractManagement dto) {
        return managementService.contractSave(dto);
    }

    @ApiOperation(value = "修改租赁信息")
    @PostMapping("/contractUpdate")
    public ResultVo contractUpdate(@RequestBody ContractManagement dto) {
        return managementService.contractUpdate(dto);
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/downLoadExportCensus", method = RequestMethod.GET)
    public void downLoadExportCensus(@RequestParam(value = "isRecycle") Integer isRecycle,
                                     HttpServletResponse response
    ) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("合同信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<ContractManagementExecl> orderList = managementService.exportCensus(isRecycle);
        EasyExcel.write(response.getOutputStream(), ContractManagementExecl.class)
                .sheet("合同信息").doWrite(orderList);
    }



    @ApiOperation(value = "上传商品")
    @PostMapping("/uploadCommodity")
    public ResultVo uploadCommodity(@RequestParam("file") MultipartFile file){
        return managementService.uploadCommodity(file);
    }
}
